import layer from './layer.js'

import globalConst from '../common/globalConst.js';
var global = globalConst.GlobalConst;

class TPickGroup {
    constructor() {
        this.dis = -1;
        this.arr = [];
    }
}

class LayerManger {
    constructor() {
        this.defaultLayerName = "0";
        let layer0 = new layer.Layer(this.defaultLayerName);
        this.setCurentLayer(layer0);
        this.m_LayerLst = new Array();
        this.addLayer(layer0);
        this.lastPickGraphArr = [];
        this.lastPickGIndex = -1;
    }

    setCurentLayer(v) { this.m_pCurentLayer = v; }
    getCurentLayer() { return this.m_pCurentLayer; }
    addLayer(layer) {
        if (this.m_LayerLst.indexOf(layer) == -1) {
            this.m_LayerLst.push(layer);
        }
    }

    addLayerByInfo(layerObj) {
        let preLayer = this.findLayer(layerObj.name);
        if (!preLayer) {
            let newLayer = new layer.Layer(layerObj.obj);
            newLayer.assignInfo(layerObj);
            this.addLayer(newLayer);
        }
    }

    switchLayer(name) {
        let destLayer = null;
        this.m_LayerLst.forEach(p => {
            if (p.name == name) {
                destLayer = p;
                return;
            }
        });
        if (destLayer) {
            this.setCurentLayer(destLayer);
        }
        return destLayer;
    }


    addGraphToLayer(graph, name, posidx) {
        if (graph) {
            var pLogicLayer = this.findLayer(name);
            if (!pLogicLayer) {
                pLogicLayer = this.findLayer(this.defaultLayerName);
            }
            pLogicLayer.addGraph(graph);
        }
    }

    findLayer(name) {
        var pDstLayer = null;
        let len = this.m_LayerLst.length;
        for (let i = 0; i < len; i++) {
            let layer = this.m_LayerLst[i];
            if (layer.name == name) {
                pDstLayer = layer;
                break;
            }
        }
        return pDstLayer;
    }

    getLayerMaxIndex() {
        let len = this.m_LayerLst.length;
        return len;
    }

    appendGraph2Lst(totalLst) {
        let len = this.m_LayerLst.length;
        for (let i = 0; i < len; i++) {
            let layer = this.m_LayerLst[i];
            layer.appendGraph2Lst(totalLst);
        }
    }

    pickGraph(tPoint, mapViewInfo, picktype,pickTopFlag) {
        var destGraph = null;
        var matchInfoLst = [];
        let len = this.m_LayerLst.length;
        for (let i = len - 1; i >= 0; i--) {
            let layer = this.m_LayerLst[i];
            layer.pickGraph(tPoint, mapViewInfo, matchInfoLst, picktype)
        }
        if (!matchInfoLst || (matchInfoLst && matchInfoLst.length == 0)) {
            this.lastPickGraphArr = [];
            this.lastPickGIndex = -1;
            return null;
        }

        let matchLst = [];
        matchInfoLst.forEach(ele => {
            matchLst.push(ele.dst);
        });

        if (matchLst.length == 1){
            return matchLst[0];
        }
        
        //选中多个则比较其在图层中的前后次序，取最前面的为当前选中
        let sortGraphList = matchLst.sort(function (p1, p2) {
            let inlayIdx1 = p1.getInlayerIdx();
            let inlayIdx2 = p2.getInlayerIdx();
            if ((inlayIdx1 - inlayIdx2) < 0) {
                return 1;
            }
            else {
                return -1;
            }
        }
        );

        

        //本次拾取的跟上次的一样
        let pickSameWithLast = false;
        if (matchLst.length == this.lastPickGraphArr.length){
            pickSameWithLast = true;
            sortGraphList.forEach(ele => {
                if (this.lastPickGraphArr.indexOf(ele) == -1){
                    pickSameWithLast = false;
                    return ;
                }
            });
        }

        if (pickTopFlag){
            pickSameWithLast = false;
        }
        
        if (!pickSameWithLast)
        {
            this.lastPickGIndex = 0;
            destGraph = sortGraphList[this.lastPickGIndex];
            this.lastPickGraphArr = [];
            sortGraphList.forEach(ele => {
                this.lastPickGraphArr.push(ele);
            });
        }
        else{
            let curentIdx = this.lastPickGIndex + 1;
            if (curentIdx > sortGraphList.length - 1){
                curentIdx = 0;
            }
            destGraph = sortGraphList[curentIdx];
            this.lastPickGIndex = curentIdx;
        }
        return destGraph;
    }

    display( map, mapInfo,refreshRect) {
        let t0 = +new Date(); 
        
        map.save();
        let len = this.m_LayerLst.length;
        for (let i = 0; i < len; i++) {
            let temp = this.m_LayerLst[i];
            if (temp.getVisible()) {
                temp.display(map, mapInfo,refreshRect);
            }
        }
        map.restore();
        let t1 = +new Date(); 
        console.log("layer timeelapse:" + (t1-t0));
    }

    displayAuxGraphs(subDataMngLst,map, mapInfo){
        map.save();
        subDataMngLst.forEach(t => {
            t.display(map, mapInfo);
        });
        map.restore();
    }

    clearData() {
        var lenth = this.m_LayerLst.length;
        for (var idx in this.m_LayerLst) {
            var layer = this.m_LayerLst[idx];
            layer.clearData();
        }
        if (lenth > 1) {
            this.m_LayerLst.splice(1, lenth - 1);
        }
    }

    chkInSelRect(rectRegion, tSelect) {
        let len = this.m_LayerLst.length;
        for (let i = 0; i < len; i++) {
            let pLayer = this.m_LayerLst[i];
            pLayer.chkInSelRect(rectRegion, tSelect);
        }
    }

    getGraphIntersectSelRect(rectRegion, tSelect){
        let len = this.m_LayerLst.length;
        for (let i = 0; i < len; i++) {
            let pLayer = this.m_LayerLst[i];
            pLayer.getGraphIntersectSelRect(rectRegion, tSelect);
        }
    }

    findGraph(gid) {
        var pGraph = null;
        let len = this.m_LayerLst.length;
        for (let i = 0; i < len; i++) {
            let pLayer = this.m_LayerLst[i];
            pGraph = pLayer.findGraph(gid);
            if (pGraph) {
                break;
            }
        }
        return pGraph;
    }

    findGraphByRID(rid) {
        var pGraph = null;
        let len = this.m_LayerLst.length;
        for (let i = 0; i < len; i++) {
            let pLayer = this.m_LayerLst[i];
            pGraph = pLayer.findGraphByRID(rid);
            if (pGraph) {
                break;
            }
        }
        return pGraph;
    }

    getAllLayer() { return this.m_LayerLst; }
    getSaveLayerJsonArr() {
        let objArr = [];
        let len = this.m_LayerLst.length;
        for (let i = 0; i < len; i++) {
            let temp = this.m_LayerLst[i];
            let obj = temp.toJson();
            objArr.push(obj);
        }
        return objArr;
    }

    pickGraph_back(tPoint, mapViewInfo, picktype) {
        var destGraph = null;
        var matchLst = new Array();
        let len = this.m_LayerLst.length;
        for (let i = len - 1; i >= 0; i--) {
            let layer = this.m_LayerLst[i];
            destGraph = layer.pickGraph(tPoint, mapViewInfo, matchLst, picktype)
            if (destGraph) {
                break
            }
        }
        if (!matchLst || (matchLst && matchLst.length == 0)) {
            return null;
        }
        //先按距离进行分组
        let disGroupLst = [];
        let count = matchLst.length;
        for (let i = 0; i < count; i++) {
            let t = matchLst[i];
            let dstGroup = null;
            disGroupLst.forEach(p => {
                if (Math.abs(p.dis - t.dis) < global.ZERO_4E) {
                    dstGroup = p;
                    return;
                }
            });
            if (!dstGroup) {
                dstGroup = new TPickGroup();
                disGroupLst.push(dstGroup);
            }
            dstGroup.dis = t.dis;
            dstGroup.arr.push(t.dst);
        }

        //取距离最小的组，然后再分层判断最顶层的
        let mindisGroup = null;
        let mindis = 0;
        if (disGroupLst.length == 1) {
            mindisGroup = disGroupLst[0];
        }
        else if (disGroupLst.length > 1) {
            mindisGroup = disGroupLst[0];
            mindis = mindisGroup.dis;
            for (let i = 1; i < disGroupLst.length; i++) {
                let t = disGroupLst[i];
                if (mindis > t.dis) {
                    mindis = t.dis;
                    mindisGroup = t;
                }
            }
        }
        else {
            return null;
        }

        let mayPickObjLst = mindisGroup.arr;

        //选中多个则比较其在图层中的前后次序，取最前面的为当前选中
        let sortGraphList = mayPickObjLst.sort(function (p1, p2) {
            let inlayIdx1 = p1.getInlayerIdx();
            let inlayIdx2 = p2.getInlayerIdx();
            if ((inlayIdx1 - inlayIdx2) > 0) {
                return 1;
            }
            else {
                return -1;
            }
        }
        );
        destGraph = sortGraphList[sortGraphList.length - 1];

        return destGraph;
    }

    pickAllGraphsOnPoint(tPoint, mapViewInfo) {
        var matchInfoLst = [];
        let len = this.m_LayerLst.length;
        for (let i = len - 1; i >= 0; i--) {
            let layer = this.m_LayerLst[i];
            layer.pickGraph(tPoint, mapViewInfo, matchInfoLst)
        }
        if (!matchInfoLst || (matchInfoLst && matchInfoLst.length == 0)) {
            this.lastPickGraphArr = [];
            this.lastPickGIndex = -1;
            return null;
        }

        let matchLst = [];
        matchInfoLst.forEach(ele => {
            matchLst.push(ele.dst);
        });
        
        //选中多个则比较其在图层中的前后次序，取最前面的为当前选中
        let sortGraphList = matchLst.sort(function (p1, p2) {
            let inlayIdx1 = p1.getInlayerIdx();
            let inlayIdx2 = p2.getInlayerIdx();
            if ((inlayIdx1 - inlayIdx2) < 0) {
                return 1;
            }
            else {
                return -1;
            }
        }
        );
        return sortGraphList;
    }
}

export default { LayerManger }